#include "raider.h"
#include <math.h>
#include <ctime>
#include <iostream>
#define PI 3.14159265


#pragma region Base
raider::raider() {
}


raider::~raider() {
}
#pragma endregion



#pragma region Publics

	void raider::Initialize(sf::RenderWindow* window, Player* player, RaiderBulletHandler* raiderBulletHandler, ltbl::Light_Point* muzzleFlash, sf::Texture* image) {
		m_renderWindow = window;
		m_player = player;
		m_muzzleFlash = muzzleFlash;
		m_raiderBulletHandler = raiderBulletHandler;

		// Randomize the raiders weapon
		srand(time(0));
		typeChance = rand()%100;
		//std::cout << typeChance << std::endl;
		// Pistol
		if (typeChance <= 80) {
			m_weaponType = 0;
		}
		// Shotgun
		if (typeChance > 80 &&  typeChance <= 90) {
			m_weaponType = 1;
		}
		// Machinegun
		if (typeChance > 90 && typeChance <= 99) {
			m_weaponType = 2;
		}
		
		//m_image = new sf::Texture();
		m_image = image;

		m_sprite = new sf::Sprite();
		m_sprite->setTexture(*m_image);

		m_isBusy = false;

		m_clock = new sf::Clock();
		m_shootClock = new sf::Clock();

		m_sprite->setOrigin(m_image->getSize().x/2, m_image->getSize().y/2);

		m_health = 6;
		playerDown = false;
		
		speed = 50.0f;
		

		m_animation = new Animation();
		m_animation->Initialize("bilder/Raider/Raider_Walk_Animation.png", sf::Vector2f(122, 80), 7, 12);
		m_legs = m_animation->GetSprite();
		m_legs->setOrigin(61.0f, 40.0f);
		
	}

	void raider::Update() {
		timeSinceLastFrame = m_clock->restart();
		playerAngle = atan2(((m_player->m_playerSprite->getPosition().y) - m_sprite->getPosition().y), ((m_player->m_playerSprite->getPosition().x) - m_sprite->getPosition().x));
		//playerAngle = atan2(((m_sprite->getPosition().y) - m_player->m_playerSprite->getPosition().y), ((m_sprite->getPosition().x) - m_player->m_playerSprite->getPosition().x));
		//shipAngle = atan2(((m_sprite->getPosition().y) - 720), ((m_sprite->getPosition().x) - 1280));
		shipAngle = atan2((720 - (m_sprite->getPosition().y)), (1280 - (m_sprite->getPosition().x)));


		raiderPos = sf::Vector2f(m_sprite->getPosition().x, m_sprite->getPosition().y);
		
		
		// Distance between raiders and (ship)
		distanceXship = 1280 - m_sprite->getPosition().x;
		if(distanceXship < 0){distanceXship *= -1;}

		distanceYship = 720 - m_sprite->getPosition().y;
		if(distanceYship < 0){distanceYship *= -1;}
		
		// Distance between raiders and player
		distanceXplayer = m_player->m_playerSprite->getPosition().x - m_sprite->getPosition().x;
		if(distanceXplayer < 0){distanceXplayer *= -1;}

		distanceYplayer = m_player->m_playerSprite->getPosition().y - m_sprite->getPosition().y;
		if(distanceYplayer < 0){distanceYplayer *= -1;}
		
		

		//std::cout << playerAngle << "    " << m_sprite->getPosition().x << "  " << m_sprite->getPosition().y << std::endl;
		// Calculate distance to player and ship
		distancePlayer = sqrt(distanceXplayer*distanceXplayer + distanceYplayer*distanceYplayer);
		distanceShip = sqrt(distanceXship*distanceXship + distanceYship*distanceYship);

		//std::cout << distancePlayer << "   " << distanceShip << std::endl;
		
		

		

		// Move
		if(playerDown == false) {
			if(distancePlayer < distanceShip){
				if(distancePlayer < 200.0f) {
					speed = 0;
					m_sprite->setRotation(playerAngle * 180 / PI * 1 + 180);
					attack_player();
				}
				m_sprite->setRotation(playerAngle * 180 / PI * 1 + 180);
				m_sprite->move(speed * cos(playerAngle) * timeSinceLastFrame.asSeconds(),
				speed * sin(playerAngle) * timeSinceLastFrame.asSeconds());
			}
		}
		
		if(distancePlayer >= distanceShip || playerDown == true)
		{
			if(distanceShip < 400)
			{
				speed = 0;
				m_sprite->setRotation(shipAngle * 180 / PI * 1 + 180);
				attack_ship();
			}
			m_sprite->setRotation(shipAngle * 180 / PI * 1 + 180);
			m_sprite->move(speed * cos(shipAngle) * timeSinceLastFrame.asSeconds(),
			speed * sin(shipAngle) * timeSinceLastFrame.asSeconds());
		}

		m_shootTimer += m_shootClock->restart();
		
		
		if(speed == 50)
		{
			m_legs->setPosition(m_sprite->getPosition());
			m_legs->setRotation(m_sprite->getRotation() + 180);
			m_renderWindow->draw(*m_legs);
			m_animation->Update(timeSinceLastFrame.asSeconds());
		}

		speed = 50.0f;

		m_renderWindow->draw(*m_sprite);

	}

	void raider::attack_player()
	{
		if(m_weaponType == 0 && m_shootTimer.asSeconds() >= 1.5f)
		{
			m_raiderBulletHandler->addBullet(playerAngle, m_sprite->getPosition());			
			m_shootTimer -= m_shootTimer;
		}

		if(m_weaponType == 1 && m_shootTimer.asSeconds() >= 2.0f)
		{
			m_raiderBulletHandler->addBullet(playerAngle, m_sprite->getPosition());			
			m_shootTimer -= m_shootTimer;
		}

		if(m_weaponType == 2 && m_shootTimer.asSeconds() >= 0.35f)
		{
			m_raiderBulletHandler->addBullet(playerAngle, m_sprite->getPosition());			
			m_shootTimer -= m_shootTimer;
		}
	}

	void raider::attack_ship()
	{
		if(m_weaponType == 0 && m_shootTimer.asSeconds() >= 1.5f)
		{
			m_raiderBulletHandler->addBullet(shipAngle, m_sprite->getPosition());			
			m_shootTimer -= m_shootTimer;
		}

		if(m_weaponType == 1 && m_shootTimer.asSeconds() >= 2.0f)
		{
			m_raiderBulletHandler->addBullet(shipAngle, m_sprite->getPosition());			
			m_shootTimer -= m_shootTimer;
		}

		if(m_weaponType == 2 && m_shootTimer.asSeconds() >= 0.75f)
		{
			m_raiderBulletHandler->addBullet(shipAngle, m_sprite->getPosition());			
			m_shootTimer -= m_shootTimer;
		}
	}


	void raider::Cleanup() {

	//delete m_image;
	delete m_sprite;
	delete m_clock;
	delete m_shootClock;
	delete m_animation;
	//delete m_renderWindow;
	}

	void raider::Spawn(float xPos, float yPos) {
		m_sprite->setPosition(xPos, yPos);
		m_isBusy = true;
	}

	bool raider::IsBusy() { 
		return m_isBusy;
	}

	void raider::Destroy() {
		m_isBusy = false; 
	}


	sf::FloatRect raider::GetRect() { 
		return sf::FloatRect(m_sprite->getPosition().x, m_sprite->getPosition().y,
			m_sprite->getPosition().x + m_sprite->getGlobalBounds().width, m_sprite->getPosition().y + m_sprite->getGlobalBounds().height);
	}

	int raider::WeaponType() {
		return m_weaponType;
	}




#pragma endregion

